package com.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.power.domain.Notice;
import com.power.mapper.NoticeMapper;
import com.power.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author dingchunlin
* @description 针对表【notice】的数据库操作Service实现
* @createDate 2024-07-10 15:12:14
*/
@Service
@CacheConfig(cacheNames = "com.power.service.impl.NoticeServiceImpl")
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService{

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 保存通知
     * @param notice 通知对象
     * @return 是否保存成功
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = "'topWxTopNoticeList'"),
            @CacheEvict(key = "'wxAllNoticeList'")
    })
    public Boolean saveNotice(Notice notice) {
        notice.setShopId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        return noticeMapper.insert(notice) > 0;
    }

    @Override
    @Caching(evict = {
            @CacheEvict(key = "'topWxTopNoticeList'"),
            @CacheEvict(key = "'wxAllNoticeList'")
    })
    public boolean remove(Wrapper<Notice> queryWrapper) {
        return super.remove(queryWrapper);
    }

    /**
     * 修改通知
     *
     * @param notice 通知对象
     * @return 是否修改成功
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = "'topWxTopNoticeList'"),
            @CacheEvict(key = "'wxAllNoticeList'")
    })
    public Boolean modifyNotice(Notice notice) {
        notice.setUpdateTime(new Date());
        return noticeMapper.updateById(notice) > 0;
    }

    /**
     * 查询置顶通知列表
     * @return 置顶通知列表
     */
    @Override
    @Cacheable(key = "'topWxTopNoticeList'")
    public List<Notice> queryTopWxTopNoticeList() {
        List<Notice> noticeList = noticeMapper.selectList(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getStatus, 1)
                .eq(Notice::getIsTop, 1)
                .orderByDesc(Notice::getCreateTime));
        return noticeList;
    }

    /**
     * 查询微信公众号通知列表
     *
     * @return 微信公众号通知列表
     */
    @Override
    @Cacheable(key = "'wxAllNoticeList'")
    public List<Notice> queryWxAllNoticeList() {
        List<Notice> noticeList = noticeMapper.selectList(new LambdaQueryWrapper<Notice>()
               .eq(Notice::getStatus, 1)
               .orderByDesc(Notice::getCreateTime));
        return noticeList;
    }

}




